package com.dalin.platform.loa.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.dalin.common.utils.StringUtils;
import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.framework.vos.CheckboxTreeVo;
import com.dalin.framework.vos.TreeVo;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.condition.LoaOperateCondition;
import com.dalin.platform.loa.condition.LoaOperateHeaderCondition;
import com.dalin.platform.loa.condition.LoaRoleOperateCondition;
import com.dalin.platform.loa.condition.MenuCondition;
import com.dalin.platform.loa.model.LoaMenuEntity;
import com.dalin.platform.loa.model.LoaOperateEntity;
import com.dalin.platform.loa.model.LoaOperateHeaderEntity;
import com.dalin.platform.loa.model.LoaRoleEntity;
import com.dalin.platform.loa.model.LoaRoleOperateEntity;
import com.dalin.platform.loa.service.LoaService;
import com.dalin.platform.pub.PlatContext;


/*****************************************************************************
 * @content: 查询角色菜单授权树                                               *
 * @author : 那建林                                                          *
 * @crtDate: 2009-07-29                                                     *
 ****************************************************************************/
public class RoleMenuQueryAction extends StrutsTwoAction {
	private LoaService loaService;
	private String parentId;
	private Long roleId;
	private String flag;
	
	public String execute() throws Exception {
		super.printInfo("", "===== this action begin");
		try {
			Long projectId = getCurrentUser().getProjectId();
			if (StringUtils.isBlank(parentId) || LoaConst.BASE_PARENT_ID.toString()
					.equals(parentId.toString())) {
				List<TreeVo> rtnList = new ArrayList<TreeVo>();
				for (CheckboxTreeVo vo : getMenuTreeList(0 - projectId)) {
					rtnList.add(vo);
				}
				if (projectId != 1L) {
					TreeVo platTree = new TreeVo();
					platTree.setId("menu~-1");
					platTree.setText("cutedalin开发平台");
					rtnList.add(platTree);
				}
				setResContent(rtnList);
			} else {
				String idFlag = parentId.split("~")[0];
				Long pId = Long.parseLong(parentId.split("~")[1]);
				if ("operate".equals(flag)) {
					if ("menu".equals(idFlag)) {
						setResContent(getButtonTreeList(pId, Long.valueOf(LoaConst.BASE_PARENT_ID)));
					} else {
						setResContent(getButtonTreeList(null, pId));
					}
				} else if ("header".equals(flag)) {
					setResContent(getOperateHeaderTreeList(pId));
				} else {
					setResContent(getMenuTreeList(pId));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 某角色是否拥有该菜单权限
	private List<CheckboxTreeVo> getMenuTreeList(Long pId) throws Exception {
		List<CheckboxTreeVo> treeList = new ArrayList<CheckboxTreeVo>();
		MenuCondition menuCond = new MenuCondition();
		menuCond.setProjectId(getCurrentUser().getProjectId());
		menuCond.setParentId(pId);
		List<LoaMenuEntity> menuList = loaService
					.queryListByCond(menuCond.prepareParams());
		
		CheckboxTreeVo ctv = null;
		for (LoaMenuEntity lme : menuList) {
			ctv = new CheckboxTreeVo();
			ctv.setId("menu~" + lme.getMenuId());
			ctv.setFlag("menu");
			ctv.setText(lme.getMenuName());
			ctv.setChecked(isRoleOwningMenu(roleId, lme));
			
			// 判断是否为叶子结点
			menuCond.setParentId(lme.getMenuId());
			List<LoaMenuEntity> childrenList = loaService
					.queryListByCond(menuCond.prepareParams());
			if (StringUtils.isBlank(childrenList)) {
				if (StringUtils.isBlank(lme.getLoaOperateEntitys())) {
					ctv.setLeaf(true);
				} else {
					ctv.setFlag("operate");
				}
			}
			treeList.add(ctv);
		}
		return treeList;
	}
	
	// 操作
	private List<CheckboxTreeVo> getButtonTreeList(Long menuId, Long parentId) throws Exception {
		List<CheckboxTreeVo> treeList = new ArrayList<CheckboxTreeVo>();
		LoaOperateCondition cond = new LoaOperateCondition();
		cond.setMenuId(menuId);
		cond.setParentOperateId(parentId);
		List<LoaOperateEntity> operateList = loaService
					.queryListByCond(cond.prepareParams());
		
		CheckboxTreeVo ctv = null;
		for (LoaOperateEntity loe : operateList) {
			ctv = new CheckboxTreeVo();
			ctv.setId("operate~" + loe.getOperateId());
			ctv.setFlag("operate");
			ctv.setText(loe.getOperateName() + " (按钮)");
			ctv.setChecked(isRoleOwningOperate(loe.getOperateId()));
			
			cond.setParentOperateId(loe.getOperateId());
			List childrenList = loaService
					.queryListByCond(cond.prepareParams());	
			if (StringUtils.isBlank(childrenList)) {
				if (StringUtils.isBlank(loe.getLoaOperateHeaderEntitys())) {
					ctv.setLeaf(true);
				} else {
					ctv.setFlag("header");
				}
			}
			treeList.add(ctv);
		}
		return treeList;
	}
	
	// 列头
	private List<CheckboxTreeVo> getOperateHeaderTreeList(Long operateId) throws Exception {
		List<CheckboxTreeVo> treeList = new ArrayList<CheckboxTreeVo>();
		LoaOperateHeaderCondition cond = new LoaOperateHeaderCondition();
    	cond.setOperateId(operateId);
		List<LoaOperateHeaderEntity> headerList = loaService
					.queryListByCond(cond.prepareParams());
		
		CheckboxTreeVo ctv = null;
		for (LoaOperateHeaderEntity lghe : headerList) {
			ctv = new CheckboxTreeVo();
			ctv.setId("header~" + lghe.getLohId());
			ctv.setText(lghe.getGridHeaderEntity().getLabelName() + " (列头)");
			ctv.setChecked(isRoleOwningHeader(lghe.getLohId()));
			ctv.setLeaf(true);
			treeList.add(ctv);
		}
		return treeList;
	}
	
	
	// 某角色是否拥有该菜单权限
	private boolean isRoleOwningMenu(Long roleId, LoaMenuEntity menuEnty) {
		LoaRoleEntity roleEnty = null;
		Set roleEntySet = menuEnty.getLoaRoleEntitys();
		for (Iterator it = roleEntySet.iterator(); it.hasNext();) {
			roleEnty = (LoaRoleEntity) it.next();
			if (roleId.equals(roleEnty.getRoleId())) {
				return true;
			}
		}
		return false;
	}
	
	private boolean isRoleOwningOperate(Long operateId) {
		LoaRoleOperateCondition cond = new LoaRoleOperateCondition();
		cond.setOperateId(operateId);
		cond.setRoleId(roleId);
		List<LoaRoleOperateEntity> rreList = PlatContext.getDao()
				.queryObjsToPages(cond.prepareParams()
				.getPreparedPageQuery());
		if (StringUtils.isBlank(rreList)) {
			return false;
		}
		return true;
	}
	
	
	private boolean isRoleOwningHeader(Long lohId) {
		LoaOperateHeaderEntity lohEnty = (LoaOperateHeaderEntity) PlatContext
				.getDao().findObjById(LoaOperateHeaderEntity.class, lohId);
		LoaRoleOperateCondition cond = new LoaRoleOperateCondition();
		cond.setRoleId(roleId);
		cond.setOperateId(lohEnty.getOperateId());
		List<LoaRoleOperateEntity> rreList = PlatContext.getDao()
				.queryObjsToPages(cond.prepareParams()
				.getPreparedPageQuery());
		if (!StringUtils.isBlank(rreList)) {
			LoaRoleOperateEntity enty = rreList.get(0);
			if (!StringUtils.isBlank(enty.getLohIds())) {
				String[] lohArray = enty.getLohIds().split(",");
				for (String id : lohArray) {
					if (id.equals(lohId + "")) {
						return true;
					}
				}
			}
		}
		return false;
	}
	/*************************************************************************
	 * 以下是bean属性的set/get方法
	 */
	public LoaService getLoaService() {
		return loaService;
	}

	public void setLoaService(LoaService loaService) {
		this.loaService = loaService;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}
